package cn.goldgrid.web;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class GlobalFilter implements Filter {

	private ServletContext context;
	
	protected String encoding = "UTF-8";
	
	public final static String REQUEST_URI = "request_uri";
	

	public void init(FilterConfig cfg) throws ServletException {
		this.context = cfg.getServletContext();
		this.encoding = cfg.getInitParameter("encoding");
		if(encoding != null){
			this.encoding = RequestContext.encoding;
		}
		
	}
	
	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain chain) throws IOException, ServletException {

		HttpServletRequest request = (HttpServletRequest)req;
		
		HttpServletResponse response = (HttpServletResponse)res;
		
		RequestContext rc = RequestContext.begin(this.context, request, response);
		
		chain.doFilter(rc.request(), rc.response());
	}
	
	public void destroy() {
	}
}
